<?php 
class ExpmaxsController extends AppController {

	var $name = 'Expmaxs';
	
	var $uses = array('Opt','Checkin',
        'Totalcheckin','Test','ZnumberLocal','Result',
        'Newcheckin','Friendship','Testing','NMaxUserLocationCsTb','F','NLocationC',
        'Testingfull','Checkinstrain',
        'NMaxUserLocationTrainC','LocationTrain','F',
        'TrainLocationMaxForUsersTable',
        'TrainTonghopWithoutF',
        'Location','TrainTonghopmaxWithoutF',
        'FulltrainTonghopWithoutF','LocationNumck',
        'FulltrainsULocationTable',
        'LocationNumcksTb',
        'TrainsULocationTable',
        'Fullmax10round');
	var $helpers = array('Html', 'Form',  'Ajax','flashChart');
	var $components = array('Inf');
	
    /**
     * refer from http://pastebin.com/P711segA
     * */
    function t(){
        
    }
    function calculateDistance($lat_a,$lat_b,$long_a,$long_b) {
        if($lat_a==$lat_b&&$long_a==$long_b)
            return 0;
            
        $lat_a = $lat_a * PI()/180;
        $lat_b = $lat_b * PI()/180;
        $long_a = $long_a * PI()/180;
        $long_b = $long_b * PI()/180;
        $distance =
                acos(
                        sin($lat_a ) * sin($lat_b) +
                        cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
                ) * 6371;
       
        return $distance;
    }
    
    /**
     * Xet xem su di chuyen cua user co bi anh huong boi ban khong
     * (Ban co den noi do truoc?)
     * */
    function capnhatinfed(){
        /*
        $lat1=-122.411;
        $long1=37.7802;
        $lat2=-122.389;
        $long2=37.6183;*/
        
        //khong gioi han thoi gian xu ly
        set_time_limit( 3600000);
        ini_set('memory_limit', '-1');
        $this->Newcheckin->recursive=-1;
        $dulieu=$this->Newcheckin->find('all',array(
            'order'=>array('location_id'=>'asc','time'=>'asc')));
        //print_r($dulieu);
       /*
        $lat1=-122.389;
        $long1=37.7982;
        $lat2=-122.389;
        $long2=37.6183;
        */
        $n=count($dulieu);
        $dem=array();
        $tap_user_dentruoc=array();
        for($i=1;$i<1500;$i++){
            if($dulieu[$i]['Newcheckin']['location_id']
                ==$dulieu[$i-1]['Newcheckin']['location_id']){
                $this->F->recursive=-1;
                $friends=$this->F->find('list',
                    array('conditions'=>array('user_id'=>$dulieu[$i]['Newcheckin']['user_id']),
                          'fields'=>array('withuser','withuser')));
                $friends=array_values($friends);
                $giao=array_intersect($tap_user_dentruoc,$friends);
                if(!empty($giao))
                   // $dem[]=$i;
                    $this->Opt->query("update newcheckins 
                        set influenced=1,time=time
                        where user_id=".$dulieu[$i]['Newcheckin']['user_id']."
                            and location_id='".$dulieu[$i]['Newcheckin']['location_id']."'
                            and time='".$dulieu[$i]['Newcheckin']['time']."'");
                $tap_user_dentruoc[]=$dulieu[$i]['Newcheckin']['user_id'];
              //  print_r($tap_user_dentruoc);
//                print_r($friends);
//                print_r($giao);
//                print_r(empty($giao));
        
            }
            else{
                $tap_user_dentruoc=array();
            }
        }
     //   print_r($dem);
       	$this->Session->setFlash(__('run finished', true));
			
    //    $giatri=$this->calculateDistance($lat1,$lat2,$long1,$long2);
        $this->set(compact('lat1','long1','lat2','long2','giatri','dulieu'));
    }
    /**
     * Tinh khoang cach giua 2 lan checkins lien tuc
     * chay 1 lan, kha lau
     * */
     
    function chaythu(){
        /*
        $lat1=-122.411;
        $long1=37.7802;
        $lat2=-122.389;
        $long2=37.6183;*/
        
        //khong gioi han thoi gian xu ly
        set_time_limit( 360000);
        ini_set('memory_limit', '-1');
        $this->Newcheckin->recursive=-1;
     //   $dulieu=$this->Newcheckin->find('all',array(
        //    'conditions'=>array('id > ?'=>array('126326'))));
       $dulieu=$this->Newcheckin->find('all',array('order'=>array('id'=>'asc')));
       
        //print_r($dulieu);
       /*
        $lat1=-122.389;
        $long1=37.7982;
        $lat2=-122.389;
        $long2=37.6183;
        */
        $d=0;
        $n=count($dulieu);
        for($i=1;$i<$n;$i++){
            if($dulieu[$i]['Newcheckin']['user_id']
                ==$dulieu[$i-1]['Newcheckin']['user_id']){
                $d=$this->calculateDistance(
                        $dulieu[$i-1]['Newcheckin']['x'],$dulieu[$i]['Newcheckin']['x'],
                        $dulieu[$i-1]['Newcheckin']['y'],$dulieu[$i]['Newcheckin']['y']);
                if($d!=0)
                    $this->Opt->query("update newcheckins 
                        set distance='".$d."', time='".$dulieu[$i]['Newcheckin']['time']."'
                        where user_id='".$dulieu[$i]['Newcheckin']['user_id']."'
                            and location_id='".$dulieu[$i]['Newcheckin']['location_id']."'
                            and time='".$dulieu[$i]['Newcheckin']['time']."'");
            }
        }
       	$this->Session->setFlash(__('run finished', true));
			
//        $giatri=$this->calculateDistance($lat1,$lat2,$long1,$long2);
        $this->set(compact('dulieu'));
    }
   /**
    * Tim ds cac nut anh huong cuc dai voi so nut influential toi thieu
    * @param    #idtest
    * */
    function maxinf_Location($idtest){
        //Ds nhung nut anh huong
     ini_set('max_execution_time', 3600);
    $this->Opt->recursive=-1;
    $opts1=$this->Opt->find('all',array(
        'conditions'=>array('test_id'=>$idtest,'type'=>1)
        ));
    
    if(empty($opts1)){
        $uf=$this->Result->find('list',array('fields'=>array('u1','u1'),'conditions'=>array('test_id'=>$idtest)));
        $u2=$uf;
        //The set r of k influential users needed to find
        $r=array();
        
        //tim mang kem du lieu user and nhug ng bi anh huong cua user do
        $infnodes=$this->Result->find('all',array(
            'fields'=>array('u1','u2'),
            'conditions'=>array('test_id'=>$idtest)));
        $infnodes_daxet=$infnodes;
        
        //tim nguoi co nhieu ng bi anh huong nhat
        $max=$this->ZnumberLocal->find('first',array(
            'fields'=>array('u1'),
            'order'=>array('n'=>'desc'),
            'conditions'=>array('test_id'=>$idtest)));
        
        $max=$max['ZnumberLocal']['u1'];
        //them max vao bo da xet
        $r[]=$max;
        $maxArray=array($max=>$max);
        
        //bo bot phan tu co anh huong cao nhat ra khoi ds dang xet
        $u2=array_diff($u2,$maxArray);
        
        //khoi tao nhung nut bi anh huog boi k nut dang tim
        $u1=array();
        
        $n=count($infnodes_daxet);
        //chay het dong lap cua infnode de do
        for($i=0;$i<$n;$i++){
            if($infnodes_daxet[$i]['Result']['u1']==$max){
                $u1[]=$infnodes_daxet[$i]['Result']['u2'];
                $this->ZnumberLocal->query("
                    insert into opts(test_id, u1,u2,type) 
                        values(".$idtest.",".$max.",
                            ".$infnodes_daxet[$i]['Result']['u2'].",1)");
                unset($infnodes_daxet[$i]);
            }
            
        }
        
        //sap xep lai chi so mang (chay tu 0) sau khi xoa phan tu
        $infnodes_daxet=array_values($infnodes_daxet);
        //bien de dem so lan chay
        //$k=0;
    
   //    $u_ed=$this->Result->find('list',array('fields'=>array('u2','u2'),'conditions'=>array('test_id'=>$idtest)));
    
 //   $m=0;
    //    $uf2=$this->Result->find('list',array('fields'=>array('u2','u2'),'conditions'=>array('test_id'=>$idtest)));
      //  $uf2=array_values($uf2);
       while(1){
            $maxnext=$this->Maxdiff($u1,$infnodes_daxet);
           // print_r('thu '.$j.'la'.$maxnext);
           //$k++;
            if($maxnext==-1)
                break;
            $r[]=$maxnext;
            //print_r($maxnext);
      //      print_r($maxnext);    
            $n=count($infnodes_daxet);
        //chay het dong lap cua infnode de do, tim anh huong cua nut
            for($i=0;$i<$n;$i++){
                if($infnodes_daxet[$i]['Result']['u1']===$maxnext){
                    $u1[]=$infnodes_daxet[$i]['Result']['u2'];
                   $this->ZnumberLocal->query("
                       insert into opts(test_id, u1,u2,type) 
                        values(".$idtest.",".$maxnext.",
                            ".$infnodes_daxet[$i]['Result']['u2'].",1)");
            
                    unset($infnodes_daxet[$i]);
                }
            }
            //sap xep lai chi so mang (chay tu 0) sau khi xoa phan tu
             ini_set('max_execution_time', 3600);
            $infnodes_daxet=array_values($infnodes_daxet);
            //print_r('lan chay thu:'.$k);
            //print_r($infnodes_daxet);
            //loc qua nhung phan tu trung nhau
            $u1=array_unique($u1);
         //   $m++;
       }
       //print_r('so lan chay cua '.$m);
       //print_r('tong so lan chay:'.$k);
  //     print_r($r);
    
    //     print_r('in nhung cai con lai');
      //      print_r($infnodes_daxet);
        //    print_r($u1);
       //print_r($r);  
        $ufed=$this->Result->find('all',array('fields'=>array('u1','u2'),
            'conditions'=>array('test_id'=>$idtest,'u1'=>$r)));
   //   print_r($ufed);
    
        
        
        
        /** ****************************************************************
         * chay voi giai thuat BASE-LINE
         * ////////////////////////////////////////////////////////////////*/
        
        //chay voi giai thuat BASE-LINE
        $nodes_order=$this->ZnumberLocal->find('all',array(
            'fields'=>array('u1','n'),
            'order'=>array('n'=>'desc'),
            'conditions'=>array('test_id'=>$idtest)));   
        //dung cho chay BASE-LINE
        $infnodes_daxet_highest=$infnodes;
     //   print_r($nodes_order);
        
        //chứa kết quả
        $r_highest=array();
        //Gồm những nút bị ảnh hưởng bởi nút đã xét
        $u_highest=array();
        //Gồm những nút ảnh hưởng ban đầu
        $u_ed=$this->Result->find('list',array(
            'fields'=>array('u2','u2'),'conditions'=>array('test_id'=>$idtest)));
        //Sắp xếp lại chỉ số mảng
        $u_ed=array_values($u_ed);
        //chạy cho đến khi những nút ban đầu cần xét hết
        while(!empty($u_ed)){
            $r_highest[]=$nodes_order[0]['ZnumberLocal']['u1'];
            $n=count($infnodes_daxet_highest);
            for($i=0;$i<$n;$i++){
                if($infnodes_daxet_highest[$i]['Result']['u1']==
                        $nodes_order[0]['ZnumberLocal']['u1']){
                    $u_highest[]=$infnodes_daxet_highest[$i]['Result']['u2'];
                    $this->ZnumberLocal->query("
                       insert into opts(test_id, u1,u2,type) 
                        values(".$idtest.",".$infnodes_daxet_highest[$i]['Result']['u1'].",
                            ".$infnodes_daxet_highest[$i]['Result']['u2'].",2)");
            
                    unset($infnodes_daxet_highest[$i]);
                }
            }
            $u_ed=array_diff($u_ed,$u_highest);
            unset($nodes_order[0]);
            $nodes_order=array_values($nodes_order);
            $infnodes_daxet_highest=array_values($infnodes_daxet_highest);
            
     //       print_r($infnodes_daxet_highest);
        
        }
       // print_r($r_highest);
       
       
           $opts1=$this->Opt->find('all',array(
                'conditions'=>array('test_id'=>$idtest,'type'=>1)
            ));
       }
     
       $opts2=$this->Opt->query("select u1,u2
        from opts Opt where type=2 and test_id=".$idtest);
        
      // print_r($opts2);
       $this->set(compact('opts1','opts2','idtest'));
      //  print_r($opts);   
    }
    function chart($idtest){
        $opts=$this->Opt->find('all',array(
            'conditions'=>array('test_id'=>$idtest,'type'=>1),
            'fields'=>array('u1','u2')));
     
     $u1=$this->Opt->find('list',array(
            'conditions'=>array('test_id'=>$idtest,'type'=>2),
            'fields'=>array('u2','u2')));;
            print_r(count($u1));
        //$h=array(1,2,3,4,5,6);
        //used algorithm 1
        $dem=0;
        $temp;
        $h=array();
        $h[0]=0;
        $h1=array();
        $n=count($opts);
     //print_r($opts);
            for($i=0;$i<$n;$i++){
               if($i==0){
        //            $dem=1;
                    $temp=$opts[0]['Opt']['u1'];
                    $h1[]=$opts[0]['Opt']['u2'];
               }
               else{
                    if($temp==$opts[$i]['Opt']['u1']){
                        if($i==$n-1){
                           $h1[]=$opts[$i]['Opt']['u2'];
                           $h[]=count(array_unique($h1, SORT_REGULAR));
                        }
                        else
                           $h1[]=$opts[$i]['Opt']['u2'];
                            
                        
                    }else{
                        
                        
                     //   $h1=array_unique($h1);
                        $h[]=count(array_unique($h1, SORT_REGULAR));
                        print_r($h1);
                        $h1[]=$opts[$i]['Opt']['u2'];
                        $temp=$opts[$i]['Opt']['u1'];
                         if($i==$n-1){
                            $h[]=count(array_unique($h1, SORT_REGULAR));;
                            
                         }
                    }
                        
               }
               
            }
        //print_r($i);
        //print_r($h1);
        //print_r($h);
        
        //used optimized
      
       $opts2=$this->Opt->query('
                select u1, u2 from opts Opt where test_id='.$idtest.' and type=2');
     
        //$h=array(1,2,3,4,5,6);
        //used algorithm 1
        $dem=0;
        $o=array();
         $o[0]=0;
        $h2=array();
        $n=count($opts2);
    
            for($i=0;$i<$n;$i++){
               if($i==0){
                    $dem=1;
                    $temp=$opts2[0]['Opt']['u1'];
                    $h2[]=$opts2[0]['Opt']['u2'];
               }
               else{
                    if($temp==$opts2[$i]['Opt']['u1']){
                         if($i==$n-1){
                           $h2[]=$opts2[$i]['Opt']['u2'];
                           $o[]=count(array_unique($h2, SORT_REGULAR));
                        }
                        else
                           $h2[]=$opts2[$i]['Opt']['u2'];
                        
                    }else{
                        
                     
                        //$h2=array_unique($h2);
                        $o[]=count(array_unique($h2, SORT_REGULAR));;
                        $h2[]=$opts2[$i]['Opt']['u2'];
                        $temp=$opts2[$i]['Opt']['u1'];
                         if($i==$n-1){
                            $o[]=count(array_unique($h2, SORT_REGULAR));;
                            
                         }
                    }
                        
               }
               
            }
        //print_r($h1);
        $n=count($o);
        $max_value=$o[$n-1]+1;
        //print_r($o);
        $this->set(compact('h','o','max_value'));
    
        
    }
    /**
     * Ham de tim nut co khac biet lon nhat voi tap hop dang xet
     * @param u1  tap hop dang xet
     * @param infnode cac quan he
     * 
     * @return  gia tri cua nut co khac biet lon nhat
     * */
    function Maxdiff($u1,$infnodes){
        //set no limitation about time execution
        ini_set('max_execution_time', 0);
        
        $n=count($infnodes);
        $tempuser=-1;
        $indexUser=-1;
        $indexArray=0;
        $dem=0;
        $max=0;
        for($i=0;$i<$n;$i++){
            if($i==0){
             //xet o vong lap dau tien, gan dem=1 va gia tri dau tien la max tam thoi
                if(!in_array($infnodes[0]['Result']['u2'],$u1)){
                    $dem=1;
                    $max=1;
                    $indexUser=$infnodes[0]['Result']['u1'];
                    
                }
            }
            else{
                if($infnodes[$i]['Result']['u1']==$infnodes[$i-1]['Result']['u1']){
                    //truong hop cung user voi du lieu o vong lap truoc
                    if(!in_array($infnodes[$i]['Result']['u2'],$u1)){
                        $dem++;
                        if($dem>$max){
                            $indexUser=$infnodes[$i]['Result']['u1'];
                            $max=$dem;
                            $indexArray=$i;
                        }
                    }
                }else{
                    //neu qua user moi thi khoi tao dem=0;
                    $dem=0;
                    if(!in_array($infnodes[$i]['Result']['u2'],$u1))
                        $dem=1;
                }
            }
            
        }
        return $indexUser;
    }
    function test(){
    
        $this->Location->recursive=-1;
		$locations = $this->Totalcheckin->find('all',array(
            'fields'=>array('location_id','total')
           ));
        $this->set(compact('locations'));
     
     }
    /**
     * this function used for testing influential users at a Location SQL
     * @param 
     * @param
     * 
     * @return a set of influential users
     * */
      function resultl(){
        $local=$this->data['Exp']['location_id'];
        $t1=$this->data['Exp']['t1']['year']."/".
                $this->data['Exp']['t1']['month']."/".
                $this->data['Exp']['t1']['day'];
        $t2=$this->data['Exp']['t2']['year']."/".
                $this->data['Exp']['t2']['month']."/".
                $this->data['Exp']['t2']['day'];
        
                
        $users=$this->Checkin->find('list',array(
            'fields'=>array('user_id','user_id'),
            'conditions'=>array(
                'location_id'=>$local),
                'time BETWEEN ? AND ?' =>array($t1,$t2)));
        
        
        $id=$this->Test->find('first',array('fields'=>array('id'),'conditions'=>array(
            'location_id'=>$local,
            't1'=>$t1,
            't2'=>$t2)));
        $id=$id['Test']['id'];
       	if(empty($id)){
            $this->Test->create();
            $this->data['Test']['location_id']=$local;
            $this->data['Test']['t1']=$t1;
            $this->data['Test']['t2']=$t2;
            if (!$this->Test->save($this->data)) {
			     $this->Session->setFlash(__('The experiment could not be saved', true));
                 $this->redirect(array('action'=>'testl'));
            }
            $this->Session->setFlash(__('Data saved',true));
            $id=$this->Test->id;
        
            foreach ($users as $u){
                ini_set('max_execution_time', 0);
                $this->Inf->inflence_user_location_sql($local,$u,$t1,$t2,$id);
            }
        }
        $this->redirect(array('action'=>'r1',$id));
     //   $this->Result->recursive=-1;
      //  $r=$this->Result->find('all',array('conditions'=>array('test_id'=>$id)));
        //$this->set('r',$this->paginate(array('conditions'=>array('test_id'=>$id))));
        
        //$this->result->recursive=-1;
    //    $this->set('r',$this->paginate(array('test_id'=>$id)));
    }
    
    var $paginate=array('limit'=>100);
    function r1($id){
        $u1=$this->Result->find('list',array('fields'=>array('u1','u1'),'conditions'=>array('test_id'=>$id)));
        $u2=$this->Result->find('list',array('fields'=>array('u2','u2'),'conditions'=>array('test_id'=>$id)));
        $this->set('r',$this->paginate(array('test_id'=>$id)));
        $this->set(compact('u1','u2'));
    }
    /**
     * Dung location duoc di nhieu nhat de xac dinh dia diem can den
     * */
    function base1(){
        $this->Testing->recursive=-1;
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        $c=0;
        $a=array();
        for($i=0;$i<$n;$i++){
            $this->NMaxUserLocationCsTb->recursive=-1;
            $t_predicted=$this->NMaxUserLocationCsTb->find('first',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id']),
                    'fields'=>array('location_id')));
            if(
                    ($t_predicted['NMaxUserLocationCsTb']['location_id']
                        ==$t[$i]['Testing']['location_id']&&
                    $t[$i]['Testing']['yes']==1)
                ||
                    ($t_predicted['NMaxUserLocationCsTb']['location_id']
                        !=$t[$i]['Testing']['location_id']&&
                    $t[$i]['Testing']['yes']==0)
                )
                $c=1;
            else
                $c=0;
            $chinhxac=$chinhxac+$c;
          //  $a[]['l']=$t[$i]['Testing']['location_id'];
//            $a[]['p']=$t_predicted['Newcheckin']['location_id'];
//            $a[]['y']=$t[$i]['Testing']['yes'];
//            print_r($i.' '.$c);
            
        }
        print_r($chinhxac);
    }
    /**
     * dung base2 voi du doan la dia diem vua moi ghe (dia diem den som nhat)
     * */
    function base2(){
        $this->Testing->recursive=-1;
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        $c=array('l','p','y');
        for($i=0;$i<$n;$i++){
            $this->Newcheckin->recursive=-1;
            $t_predicted=$this->Newcheckin->find('first',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id']),
                    'fields'=>array('location_id'),
                    'order'=>array('id'=>'desc')));
            if(
                    ($t_predicted['Newcheckin']['location_id']
                        ==$t[$i]['Testing']['location_id']&&
                    $t[$i]['Testing']['yes']==1)
                ||
                    ($t_predicted['Newcheckin']['location_id']
                        !=$t[$i]['Testing']['location_id']&&
                    $t[$i]['Testing']['yes']==0)
                )
                $chinhxac++;
            $c[]['l']=$t[$i]['Testing']['location_id'];
            $c[]['p']=$t_predicted['Newcheckin']['location_id'];
            $c[]['y']=$t[$i]['Testing']['yes'];
        }
        //print_r($c);
        print_r($chinhxac);
    
    }
    /** danh chay training 100 rows
	chay voi full_his
    **/
    function full_his(){
		$batdau=time();
		$this->set(compact('batdau'));
        $this->Testing->recursive=-1;
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        
        for($i=0;$i<$n;$i++){
            $this->Newcheckin->recursive=-1;
            
            $t_predicted=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
                
            if(in_array($t[$i]['Testing']['location_id'],$t_predicted)){
                        $this->Testing->query("update testings set full_his=1 
							where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']."");
            }else{
                 $this->Friendship->recursive=-1;
                 $fs=$this->Friendship->find('list',
                     array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])));
                 $this->Newcheckin->recursive=-1;
                 $f_predicted=$this->Newcheckin->find('list',
                        array(
                        'conditions'=>array('user_id'=>$fs)
                    ,                    'fields'=>array('location_id','location_id')
                    ));
                    if(in_array($t[$i]['Testing']['location_id'],$f_predicted)){
                        $this->Testing->query("update testings set full_his=1 
							where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']."");
                    }
                 
                        
            }
            
           
        }
       
    }
   
    /** danh chay training 100 rows
    **/
    function b(){
        $this->Testing->recursive=-1;
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        $c=array('l','p','y');
        for($i=0;$i<$n;$i++){
            $this->Newcheckin->recursive=-1;
            
            $t_predicted=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testing']['location_id'],$t_predicted)){
                        if($t[$i]['Testing']['yes']==1){
                            $chinhxac++;
                            $s='DUNG1';
                        }else{
                            $s='SAI1';
                        }
                    }
                    else{
                    
                        if($t[$i]['Testing']['yes']==0){
                             $chinhxac++;
                            $s='DUNG0';
                        }else{
                            $s='SAI0';
                        }
                    }
            
            $c[]['l']=$t[$i]['Testing']['location_id'];
            $c[]['p']=$t_predicted;
            $c[]['y']=$t[$i]['Testing']['yes'];
             $c[]['d']=$s;
             $s='';
        }
        $this->set(compact('chinhxac','c'));
    }
   /** danh chay training tu thang 7/2010 trong csdl
    **/
    function b1(){
        $this->Testingfull->recursive=-1;
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        $c=array('l','p','y');
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted)){
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set predicted=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    /** danh chay training tu thang 7/2010 trong csdl
     * chay voi du lieu hot_regions
    **/
    function hot_his_pre1(){
        $this->Testingfull->recursive=-1;
        ///13422
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            ,'conditions'=>array('id > ?'=>array(13422))));
        $n=count($t);
        $chinhxac=0;
        $c=array('l','p','y');
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
            $this->LocationTrain->recursive=-1;
            $hots=$this->LocationTrain->find('list',array(
                'order'=>array('num_checks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted))
                    {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testingfull']['location_id'],$hots))
                          {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                          }
                 
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    
    /** danh chay training tu thang 7/2010 trong csdl
     * chay voi du lieu hot_regions
    **/
    function hot_his_fri_dis_pre1(){
        $this->Testingfull->recursive=-1;
        ///13422
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('id > ?'=>array(13422))
            ));
        $n=count($t);
        $chinhxac=0;
       
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
            $this->LocationTrain->recursive=-1;
            $hots=$this->LocationTrain->find('list',array(
                'order'=>array('num_checks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted))
                    {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_fr_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testingfull']['location_id'],$hots))
                          {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_fr_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                          }else{
                              $this->Friendship->recursive=-1;
                            $fs=$this->Friendship->find('list',
                                array('fields'=>array('withuser','withuser'),
                                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])));
                            $this->TrainTonghopWithoutF->recursive=-1;
                            $f_dis=$this->TrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
                           $this->Location->recursive=-1;
                           $dia= $this->Location->find('first',array('id'=>$t[$i]['Testingfull']['location_id']));
                           $chay=count($f_dis);
                           for($k=0;$k<$chay;$k++){
                                $khoang_cach=abs($this->calculateDistance(
                                    $f_dis[$k]['TrainTonghopWithoutF']['x'],
                                    $dia['Location']['x'],
                                    $f_dis[$k]['TrainTonghopWithoutF']['y'],
                                    $dia['Location']['y']
                                    ));
                                    if($f_dis[$k]['TrainTonghopWithoutF']['kc']>=$khoang_cach){
                                         $this->Testingfull->query("
                                         update testingfulls set hot_his_fr_pre=1
                                            where location_id='".$t[$i]['Testingfull']['location_id']."'
                                             and user_id=".$t[$i]['Testingfull']['user_id']);
                                         break;
                                    }
                                    
                                   
                           }
                           
                            
                          }
                 
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    /** danh chay training tu thang 7/2010 trong csdl
     * chay voi du lieu hot_regions
    **/
    function hot_his_fri_maxdis_dis_pre1(){
        $this->Testingfull->recursive=-1;
        ///13422
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('id > ?'=>array(13422))
            ));
        $n=count($t);
        $chinhxac=0;
       
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,                    'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
            $this->LocationTrain->recursive=-1;
            $hots=$this->LocationTrain->find('list',array(
                'order'=>array('num_checks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted))
                    {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_fr_dismax_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testingfull']['location_id'],$hots))
                          {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set hot_his_fr_dismax_pre=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                          }else{
                              $this->Friendship->recursive=-1;
                            $fs=$this->Friendship->find('list',
                                array('fields'=>array('withuser','withuser'),
                                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])));
                            $this->TrainTonghopmaxWithoutF->recursive=-1;
                            $f_dis=$this->TrainTonghopmaxWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
                           $this->Location->recursive=-1;
                           $dia= $this->Location->find('first',array('id'=>$t[$i]['Testingfull']['location_id']));
                           $chay=count($f_dis);
                           for($k=0;$k<$chay;$k++){
                                $khoang_cach=abs($this->calculateDistance(
                                    $f_dis[$k]['TrainTonghopmaxWithoutF']['x'],
                                    $dia['Location']['x'],
                                    $f_dis[$k]['TrainTonghopmaxWithoutF']['y'],
                                    $dia['Location']['y']
                                    ));
                                    if($f_dis[$k]['TrainTonghopmaxWithoutF']['kc']>=$khoang_cach){
                                         $this->Testingfull->query("
                                         update testingfulls set hot_his_fr_dismax_pre=1
                                            where location_id='".$t[$i]['Testingfull']['location_id']."'
                                             and user_id=".$t[$i]['Testingfull']['user_id']);
                                         break;
                                    }
                                    
                                   
                           }
                           
                            
                          }
                 
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    
    /** danh chay training tu thang 7/2010 trong csdl
     * xet:
     * +full history
     * +xet 20 hot locations
     * +khoang cach avg tu cac lan di chuyen
     * +khoang cach devdis tu ban cua fri
     * 
     **/
    function hot_his_maxdis_fr_avg_devdis_dis_pre1(){
        $this->Testingfull->recursive=-1;
        ///13422
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('id > ?'=>array(13422))
            ));
        $n=count($t);
        $chinhxac=0;
       
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
            $this->LocationTrain->recursive=-1;
            $hots=$this->LocationTrain->find('list',array(
                'order'=>array('num_checks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted))
                    {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set 
                                    hot_his_maxdis_fr_avg_devdis_dis_pre1=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testingfull']['location_id'],$hots))
                          {
                            $chinhxac++;
                            $this->Testingfull->query("
                                update testingfulls set 
                                    hot_his_maxdis_fr_avg_devdis_dis_pre1=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                          }else
                          {
    
       $this->TrainTonghopWithoutF->recursive=-1;
       $f_dis_user=$this->TrainTonghopWithoutF->find('first',
            array(
                'conditions'=>array(
                    'user_id'=>$t[$i]['Testingfull']['user_id'])));
       $this->Location->recursive=-1;
       $ls=$this->Location->find('all',array(
            'conditions'=>array('id'=>$t_predicted)));
       $k1=count($ls);
       //xac dinh neu tim duoc thi ngat ra khoi vong lap luon
       $ngat=0;
       for($k2=0;$k2<$k1;$k2++)
       {
            $kc1=abs($this->calculateDistance(
            $ls[$k2]['Location']['x'],
            $f_dis_user['TrainTonghopWithoutF']['x'],
            $ls[$k2]['Location']['y'],
            $f_dis_user['TrainTonghopWithoutF']['y']
                ));
            if(
                $f_dis_user['TrainTonghopWithoutF']['kc']
                    >=$kc1)
            {
                 $this->Testingfull->query("
                         update testingfulls set 
                            hot_his_maxdis_fr_avg_devdis_dis_pre1=1
                                 where location_id='".$t[$i]['Testingfull']['location_id']."'
                                      and user_id=".$t[$i]['Testingfull']['user_id']);
                 $ngat=1;
                 break;
            }
       }
       
    if($ngat==0)
    {
       $this->Friendship->recursive=-1;
       $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])));
       $this->TrainTonghopWithoutF->recursive=-1;
       $f_dis=$this->TrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
       $this->Location->recursive=-1;
       $dia= $this->Location->find('first',array('id'=>$t[$i]['Testingfull']['location_id']));
       $chay=count($f_dis);
       for($k=0;$k<$chay;$k++)
       {
            $khoang_cach=abs($this->calculateDistance(
                 $f_dis[$k]['TrainTonghopWithoutF']['x'],
                 $dia['Location']['x'],
                 $f_dis[$k]['TrainTonghopWithoutF']['y'],
                 $dia['Location']['y']
             ));
             if(($f_dis_user['TrainTonghopWithoutF']['kc'])>=$khoang_cach)
             {
                   $this->Testingfull->query("
                         update testingfulls set 
                            hot_his_maxdis_fr_avg_devdis_dis_pre1=1
                                 where location_id='".$t[$i]['Testingfull']['location_id']."'
                                      and user_id=".$t[$i]['Testingfull']['user_id']);
                    break;
             }
                                    
                                   
        }
    }
                           
                            
                          }
                 
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    
    
    /** danh chay training nguyen bo data set, dung 100 rows for testing
     * xet:
     * +full history
     * +xet 20 hot locations
     * +khoang cach avg tu cac lan di chuyen
     * +khoang cach devdis tu ban cua fri
     * 
     * 
     * sau nay la nhung cot duoc luu ket qua:
     * +hp: de luu thu nghiep voi kc trung binh o 2 latent states va o dia fuong ban be thuong o
     * (80%)
     * +hpm: voi khoang cach xa nhat (75%)
     **/
    function hot_his_maxdis_fr_avg_devdis_dis_pre100(){
        $this->Testing->recursive=-1;
        ///13422
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('yes'=>0)
            ));
        $n=count($t);
        $chinhxac=0;
       
        for($i=0;$i<$n;$i++){
            $this->Newcheckin->recursive=-1;
            
            $t_predicted=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
            $this->LocationNumck->recursive=-1;
            $hots=$this->LocationNumck->find('list',array(
                'order'=>array('num_cks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testing']['location_id'],$t_predicted))
                    {
                            $chinhxac++;
                            $this->Testing->query("
                                update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testing']['location_id'],$hots))
                          {
                            $chinhxac++;
                            $this->Testing->query("
                                update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                          }else
                          {
    
       $this->FulltrainTonghopWithoutF->recursive=-1;
       //tim dia phuong user hay toi nhat
       $f_dis_user=$this->FulltrainTonghopWithoutF->find('first',
            array(
                'conditions'=>array(
                    'user_id'=>$t[$i]['Testing']['user_id'])));
       
       $this->FulltrainsULocationTable->recursive=-1;
       $t_2latents=$this->FulltrainsULocationTable->find('all',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
           //         ,'fields'=>array('location_id','location_id')
                    ,                    'order'=>array('num_checks'=>'desc')
                    ,                    'limit'=>2
                    ));
      $this->Location->recursive=-1;
      $ls_considered=$this->Location->find('first',array(
            'conditions'=>array('id'=>$t[$i]['Testing']['location_id']),
            'fields'=>array('x','y')));
     //  print_r($t_2latents);
   //  array_values  ($t_2latents);   
       $k1=count($t_2latents);
  //    print_r($t_2latents);
       //xac dinh neu tim duoc thi ngat ra khoi vong lap luon
       $ngat=0;
       foreach($t_2latents as $b)
       {
       
            $kc1=abs($this->calculateDistance(
            $b['FulltrainsULocationTable']['x'],
            $ls_considered['Location']['x'],
            $b['FulltrainsULocationTable']['y'],
            $ls_considered['Location']['y']
                ));
            //neu khoang cach di chuyen dc so sanh voi 
            //kc-->avg move
            //max_move--> khoang cach di chuyen xa nhat
            //nho hon thi ta bao co the user se den do
            if(
                $f_dis_user['FulltrainTonghopWithoutF']['kc']
                    >=$kc1)
            {
                 $this->Testing->query("
                         update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                 $ngat=1;
                 break;
            }
       }
       
       //neu da tim duoc o tren thi bo qua
  
    if($ngat==0)
    {
       $this->Friendship->recursive=-1;
       $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])));
       $this->FulltrainTonghopWithoutF->recursive=-1;
       $f_dis=$this->FulltrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
       $chay=count($f_dis);
       for($k=0;$k<$chay;$k++)
       {
            $khoang_cach=abs($this->calculateDistance(
                 $f_dis[$k]['FulltrainTonghopWithoutF']['x'],
                  $ls_considered['Location']['x'],
                 $f_dis[$k]['FulltrainTonghopWithoutF']['y'],
                 $ls_considered['Location']['y']
             ));
             if(($f_dis_user['FulltrainTonghopWithoutF']['kc'])>=$khoang_cach)
             {
                   $this->Testing->query("
                         update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                    break;
             }
                                    
                                   
        }
    }
                           
                            
                          }
                 
                    }
                   
            
          
        }
        $this->set(compact('chinhxac'));
    }
    
    
    
     /** danh chay training nguyen bo data set, dung 100 rows for testing
     * xet:
     * +full history
     * +XET TOP 10 HOT REGIONS TRONG KHU VUC MAX_MOVE TU 2 LATENT STATES
     * +khoang cach avg tu cac lan di chuyen FROM 2 LATENT
     * +khoang cach AVG TU BAN
     * 
     * 
     * sau nay la nhung cot duoc luu ket qua:
     * +hp: de luu thu nghiep voi kc trung binh o 2 latent states va o dia fuong ban be thuong o
     * (80%)
     * +hpm: voi khoang cach xa nhat (75%)
     **/
    function hot_max_favg_2latenavg_100(){
        $this->Testing->recursive=-1;
        ///13422
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('yes'=>0)
            ));
        $n=count($t);
     //   $chinhxac=0;
        
       
        for($i=0;$i<$n;$i++){
            $this->Newcheckin->recursive=-1;
            
            $t_predicted=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
       
       //tim dia phuong user hay toi nhat             
            $this->FulltrainTonghopWithoutF->recursive=-1;
            $f_dis_user=$this->FulltrainTonghopWithoutF->find('first',
                array(
                    'conditions'=>array(
                    'user_id'=>$t[$i]['Testing']['user_id'])));
       
            
                    
            
        //    $this->LocationNumcksTb->recursive=-1;
    //        $hots_all=$this->LocationNumcksTb->find('all');
      //      print_r($hots_all);
            //$hotchay=count($hots_all);
            //khoi tao cho chua nhug dia phuong "hot"
            $hots=array();
            $timduoc=0;
            /*
            foreach($hots_all as $h)
            {
                $kc_xet=abs($this->calculateDistance(
                    $h['LocationNumcksTb']['x'],
                    $f_dis_user['FulltrainTonghopWithoutF']['x'],
                    $h['LocationNumcksTb']['y'],
                    $f_dis_user['FulltrainTonghopWithoutF']['y']));
                if($kc_xet<=$f_dis_user['FulltrainTonghopWithoutF']['max_move'])
                {
                    $timduoc++;
                    $thu=$this->HotLocationUser->find('first',
                        array('conditions'=>array(
                            'user_id'=>$t[$i]['Testing']['user_id'],
                            'location_id'=>$h['LocationNumcksTb']['location_id'])));
                    if(empty($thu))
                        $this->Location->query("
                            insert into hot_location_users
                            values('".$t[$i]['Testing']['user_id']."',
                                '".$h['LocationNumcksTb']['location_id']."',
                                '".$h['LocationNumcksTb']['num_cks']."',
                                '".$h['LocationNumcksTb']['rank']."')
                        ");       
                    $hots[]=$h['LocationNumcksTb']['location_id'];
                }
                print_r($hots);
                if($timduoc>=10)
                    break;
            }
            */
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
                    if(in_array($t[$i]['Testing']['location_id'],$t_predicted))
                    {
       //                     $chinhxac++;
                            $this->Testing->query("
                                update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                        
                    }else{
                          if(in_array($t[$i]['Testing']['location_id'],$hots))
                          {
         //                   $chinhxac++;
                            $this->Testing->query("
                                update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                          }else
                          {
    
       
       $this->FulltrainsULocationTable->recursive=-1;
       $t_2latents=$this->FulltrainsULocationTable->find('all',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
           //         ,'fields'=>array('location_id','location_id')
                    ,                    'order'=>array('num_checks'=>'desc')
                    ,                    'limit'=>2
                    ));
     
     //  print_r($t_2latents);
   //  array_values  ($t_2latents);   
       $k1=count($t_2latents);
  //    print_r($t_2latents);
       //xac dinh neu tim duoc thi ngat ra khoi vong lap luon
       $ngat=0;
       foreach($t_2latents as $b)
       {
       
            $kc1=abs($this->calculateDistance(
            $b['FulltrainsULocationTable']['x'],
            $ls_considered['Location']['x'],
            $b['FulltrainsULocationTable']['y'],
            $ls_considered['Location']['y']
                ));
            //neu khoang cach di chuyen dc so sanh voi 
            //kc-->avg move
            //max_move--> khoang cach di chuyen xa nhat
            //nho hon thi ta bao co the user se den do
            if(
                $f_dis_user['FulltrainTonghopWithoutF']['kc']
                    >=$kc1)
            {
                 $this->Testing->query("
                         update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                 $ngat=1;
                 break;
            }
       }
       
       //neu da tim duoc o tren thi bo qua
  
    if($ngat==0)
    {
       $this->Friendship->recursive=-1;
       $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])));
       $this->FulltrainTonghopWithoutF->recursive=-1;
       $f_dis=$this->FulltrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
       $chay=count($f_dis);
       for($k=0;$k<$chay;$k++)
       {
            $khoang_cach=abs($this->calculateDistance(
                 $f_dis[$k]['FulltrainTonghopWithoutF']['x'],
                  $ls_considered['Location']['x'],
                 $f_dis[$k]['FulltrainTonghopWithoutF']['y'],
                 $ls_considered['Location']['y']
             ));
             if(($f_dis_user['FulltrainTonghopWithoutF']['kc'])>=$khoang_cach)
             {
                   $this->Testing->query("
                         update testings set 
                                    hpm=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                    break;
             }
                                    
                                   
        }
    }
                           
                            
                          }
                 
                    }
                   
            
          
        }
        //$this->set(compact('chinhxac'));
    }
    
    /**
     * tinh khoang dia diem den nhieu nhat voi dia fuong can tim
     * */
    function hot_dis100(){
        $this->Testing->recursive=-1;
        ///13422
        $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('yes'=>0)
            ));
        $n=count($t);
        $chinhxac=0;
       
        for($i=0;$i<$n;$i++){
            
            $this->FulltrainTonghopWithoutF->recursive=-1;
            $f_dis_user=$this->FulltrainTonghopWithoutF->find('first',
                array(
                'conditions'=>array(
                    'user_id'=>$t[$i]['Testing']['user_id'])));
            
            //dia diem cua location_id dang xet
            $this->Location->recursive=-1;
            $ls=$this->Location->find('first',array(
                'conditions'=>array('id'=>$t[$i]['Testing']['location_id'])));
            $kc1=abs($this->calculateDistance(
            $ls['Location']['x'],
            $f_dis_user['FulltrainTonghopWithoutF']['x'],
            $ls['Location']['y'],
            $f_dis_user['FulltrainTonghopWithoutF']['y']
                ));
            if($kc1>$f_dis_user['FulltrainTonghopWithoutF']['kc'])
                $mc=0;
            else $mc=1;
            $this->Testing->query("
                update testings set avg_move='".$f_dis_user['FulltrainTonghopWithoutF']['kc']."', 
                    kc='".$kc1."', p='".$mc."'
                    where location_id='".$t[$i]['Testing']['location_id']."' and user_id='".$t[$i]['Testing']['user_id']."'
            ");
            
       }
       
       
        $this->set(compact('chinhxac'));
    }
     /**
     * chay thu nghiem voi du lieu thuc 29k rows
     * base1 dung voi checkin nhieu nhat cho viec du doan
     * */
    function base1_real(){
        $this->Testingfull->recursive=-1;
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        
        for($i=0;$i<$n;$i++){
           $this->NMaxUserLocationTrainC->recursive=-1;
            $t_predicted=$this->NMaxUserLocationTrainC->find('first',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id']),
                    'fields'=>array('location_id')
                    //,                    'order'=>array('id'=>'desc')
                    ));
            $t_predicted=$t_predicted['NMaxUserLocationTrainC']['location_id'];
             if($t[$i]['Testingfull']['location_id']==$t_predicted){
                            $chinhxac++;
                $this->Testingfull->query("
                    update testingfulls set base1_predicted=1
                         where location_id='".$t[$i]['Testingfull']['location_id']."'
                               and user_id=".$t[$i]['Testingfull']['user_id']);
                        
              }
          
        }
        $this->set(compact('chinhxac'));
    }
    
    /**
     * chay thu nghiem voi du lieu thuc 29k rows
     * base2 dung voi checkin moi nhat
     * */
    function base2_real(){
        $this->Testingfull->recursive=-1;
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')));
        $n=count($t);
        $chinhxac=0;
        
        for($i=0;$i<$n;$i++){
           $this->Checkinstrain->recursive=-1;
            $t_predicted=$this->Checkinstrain->find('first',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id']),
                    'fields'=>array('location_id'),
                    'order'=>array('id'=>'desc')));
            $t_predicted=$t_predicted['Checkinstrain']['location_id'];
             if($t[$i]['Testingfull']['location_id']==$t_predicted){
                            $chinhxac++;
                $this->Testingfull->query("
                    update testingfulls set base2_predicted=1
                         where location_id='".$t[$i]['Testingfull']['location_id']."'
                               and user_id=".$t[$i]['Testingfull']['user_id']);
                        
              }
          
        }
        $this->set(compact('chinhxac'));
    }



/** danh chay training nguyen bo data set, REAL DATA
     * xet:
     * +full history
     * +xet 20 hot locations
     * +khoang cach avg tu cac lan di chuyen
     * +khoang cach devdis tu ban cua fri
     * chay tu 21h33 ngay 22/11, xong luc 10h ngay hom sau
     * du doan chinh xac 15981/ 29,372
     * 
     * sau nay la nhung cot duoc luu ket qua:
**/
    function hot_his_maxdis_fr_avg_devdis_dis_real(){
        
$cotupdate='f80hot';
$hot_degree=5;
/**
 * f_around luu tru ket qua chay voi ban kinh xung quanh la max_move
 * lay luon cac du lieu location_id cua fri
 * */
        $this->Testingfull->recursive=-1;
        ///13422
        $t=$this->Testingfull->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            
       //     ,'conditions'=>array(
         //       'id >= ?'=>array(28928)
            //    ,'yes'=>0
            //)
            ));
        $n=count($t);
        $this->LocationTrain->recursive=-1;
        $hots=$this->LocationTrain->find('list',array(
          //      'order'=>array('num_cks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'conditions'=>array(
                    'num_checks >=? '=>array(80)//,
                //    'user_id'=>$t[$i]['Testing']['user_id']
                    )));
    
       
        for($i=0;$i<$n;$i++){
            $this->Checkinstrain->recursive=-1;
            
            $t_predicted=$this->Checkinstrain->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
            
           
            /*
            $hots=$this->LocationTrain->find('list',array(
                'order'=>array('num_checks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>20));*/
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
        
             if(in_array($t[$i]['Testingfull']['location_id'],$t_predicted))
             {
                            
                $this->Testingfull->query("
                                update testingfulls set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                        
             }else{
                if(in_array($t[$i]['Testingfull']['location_id'],$hots))
         {
             
              $this->Testing->query("
                                update testingfulls set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
         }else{
                 $this->Friendship->recursive=-1;
                $fs=$this->Friendship->find('list',
                     array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])));
            
                $this->Checkinstrain->recursive=-1;
                $f_pre=$this->Checkinstrain->find('list',
                    array(
                    'conditions'=>array('user_id'=>$fs)
                ,   'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                ));
                if(in_array($t[$i]['Testingfull']['location_id'],$f_pre))
                          {
                            
                            $this->Testingfull->query("
                                update testingfulls set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                          }else
                          {
    
       $this->TrainTonghopWithoutF->recursive=-1;
       //tim dia phuong user hay toi nhat
       $f_dis_user=$this->TrainTonghopWithoutF->find('first',
            array(
                'conditions'=>array(
                    'user_id'=>$t[$i]['Testingfull']['user_id'])));
       
       $this->TrainsULocationTable->recursive=-1;
       $t_2latents=$this->TrainsULocationTable->find('all',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])
           //         ,'fields'=>array('location_id','location_id')
                    ,                    'order'=>array('num_checks'=>'desc')
                    ,                    'limit'=>2
                    ));
      $this->LocationTrain->recursive=-1;
      $ls_considered=$this->LocationTrain->find('first',array(
            'conditions'=>array(
                'num_checks >='=>$hot_degree,
                'location_id'=>$t[$i]['Testingfull']['location_id']),
            'fields'=>array('x','y')));
     //  print_r($t_2latents);
   //  array_values  ($t_2latents);   
       $k1=count($t_2latents);
  //    print_r($t_2latents);
       //xac dinh neu tim duoc thi ngat ra khoi vong lap luon
       $ngat=0;
       
       /** so sanh khoang cach*/
       $max=0;
                  //      if($chan==0){
       if($f_dis_user['TrainTonghopWithoutF']['max_move']<=10)
            $max=10;
       else
            $max=$f_dis_user['TrainTonghopWithoutF']['max_move'];
       /** ******************/
       foreach($t_2latents as $b)
       {
       
                     
            $kc1=abs($this->calculateDistance(
                $b['TrainsULocationTable']['x'],
                $ls_considered['LocationTrain']['x'],
                $b['TrainsULocationTable']['y'],
                $ls_considered['LocationTrain']['y']
            ));
            //neu khoang cach di chuyen dc so sanh voi 
            //kc-->avg move
            //max_move--> khoang cach di chuyen xa nhat
            //nho hon thi ta bao co the user se den do
            if($max>=$kc1)
            {
                 $this->Testingfull->query("
                         update testingfulls set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                 $ngat=1;
                 break;
            }
       }
       
       //neu da tim duoc o tren thi bo qua
  
    if($ngat==0)
    {
       $this->Friendship->recursive=-1;
       $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testingfull']['user_id'])));
       $this->TrainTonghopWithoutF->recursive=-1;
       $f_dis=$this->TrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
       $chay=count($f_dis);
       for($k=0;$k<$chay;$k++)
       {
            $khoang_cach=abs($this->calculateDistance(
                 $f_dis[$k]['TrainTonghopWithoutF']['x'],
                  $ls_considered['LocationTrain']['x'],
                 $f_dis[$k]['TrainTonghopWithoutF']['y'],
                 $ls_considered['LocationTrain']['y']
             ));
             if($max>=$khoang_cach)
             {
                   $this->Testingfull->query("
                         update testingfulls set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testingfull']['location_id']."'
                                        and user_id=".$t[$i]['Testingfull']['user_id']);
                    break;
             }
                                    
                                   
        }
    }
                           
                            
                          }
                 
                    }
                   
            }
          
        }
        //$this->set(compact('chinhxac'));
    }
    
    
    
    
      /** danh chay training nguyen bo data set, dung 100 rows for testing
     * xet:
     * +full history OF USER AND FRIENDS
     * +xet 20 hot locations
     * +khoang cach avg tu cac lan di chuyen
     * +khoang cach AVG tu ban cua fri
     * 
     * 81%--->khong xet nhung dia fuong hot
     * 85%-->hot_regions num_cks>100 hoac 85
     * 
     * sau nay la nhung cot duoc luu ket qua:
     **/
    function hot_fullhis_disfr_avg_frhis_100(){
//cot de update; de tren day de co thay doi cot thi sua cho de
$cotupdate='f_op';
/**
 * 
 * danh sach cac cot de luu ket qua thu nghiem
 * +f_10_hot: nhug check-ins bao quanh khu vuc 2 latent states voi kc max_move
 * va check-in>10===>86% voi 44 R, 8W
 * +f_10_1la_hot: 1 latent, voi >10cks==>42R, 6W
 * +f_10_2la_hot: 2 latent: 1-->max, 2-->trungbinh-->86 nhu f_10_1la_hot
 * +f10_2lf__hot: 1 latent for f+u,ck>=10 87%==>43R, 6W
 * +f5_2lf__hot: 1 latent for f+u,ck>=10 88%==>44R, 6W
 * f3_2lf__hot: 1 latent for f+u,ck>=10 88%==>44R, 6W
 * f1_2lf__hot: 79%
 * */
/** xet do hot cua location, bang so check-ins, ap dung cho nhug vung bao quanh
noi ma user hay den nhat**/
$hot_degree=5;



//date_default_timezone_set('Asia/Taipei');
//$begint=time();
//$this->set(compact('begint'));
$this->Testing->recursive=-1;
        ///13422
$t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('yes'=>0)
            ));
$n=count($t);

//$demochay=array(578,636,694,752,810,
//    868,926,984,1042,1094,1157);
$demochay=array(57,115,173,231,289,
    347,405,463,521);

foreach($demochay as $demo){
    /**
     * dung 2 cot thay doi duoc la ten cut va ten hot_degree
     * */
//    da dung af de thu nghiem 61 den 89, ty le 90 den 91%
$cotupdate='per10_op'.$demo;
//dat ten cot voi tien to "e" sau moi lan chay moi
$this->Testing->query("
        ALTER TABLE  `testings` ADD  ".$cotupdate." INT NOT NULL ;
    ");

$this->LocationNumcksTb->recursive=-1;
$hots=$this->LocationNumcksTb->find('list',array(
                'order'=>array('num_cks'=>'desc'),
                'fields'=>array('location_id','location_id'),
                'limit'=>$demo
           //     'conditions'=>array(
             //       'num_cks >=? '=>array($demo)//,
                //    'user_id'=>$t[$i]['Testing']['user_id']
               //     )
                ));
            //$t_predicted=$t_predicted['Newcheckin']['location_id'];;
 $h1= $this->LocationNumcksTb->find('all',array(
          //      'order'=>array('num_cks'=>'desc'),
                'fields'=>array('location_id','x','y'),
                'order'=>array('num_cks'=>'desc'),
                'limit'=>300
                //'conditions'=>array(
                
                    //'num_cks >=? '=>array()//,
                //    'user_id'=>$t[$i]['Testing']['user_id']
                    //)
                    ));



/** bat dau FOR 1*/
for($i=0;$i<$n;$i++){
    
   $this->Newcheckin->recursive=-1;
            
   $t_predicted=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                    ));
  //  $hots=array();
    
   
/**
 * if so 1
 * */        
   if(in_array($t[$i]['Testing']['location_id'],$t_predicted))
   {
       
        $this->Testing->query("
                                update testings set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
                        
    }
    /**
 * ELSE so 1
 * */else
    {
        /**
 * if so 2
 * */
         if(in_array($t[$i]['Testing']['location_id'],$hots))
         {
             
              $this->Testing->query("
                                update testings set 
                                    ".$cotupdate."=1
                                    where location_id='".$t[$i]['Testing']['location_id']."'
                                        and user_id=".$t[$i]['Testing']['user_id']);
          }
          /**
 * else so 2
 * */
          else          
          {
       //vi co 3 truong hop xet ben duoi (khong dung if de tranh bi long vong) nen dung $ngat
       
                $ngat=0;                     
                $this->Friendship->recursive=-1;
                $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])));
       
                $this->Newcheckin->recursive=-1;
                $f_predicted=$this->Newcheckin->find('list',
                        array(
                        'conditions'=>array('user_id'=>$fs)
                    ,                    'fields'=>array('location_id','location_id')
                    ));
       /**
 * if so 3
 * */
                if(in_array($t[$i]['Testing']['location_id'],$f_predicted)){
                        $this->Testing->query("update testings set 
                            ".$cotupdate."=1 
							where location_id='".$t[$i]['Testing']['location_id']."'
                                    and user_id=".$t[$i]['Testing']['user_id']."");
                }
                 /**
 * ELSE so 3
 * */
                else{
                       
                        $this->FulltrainTonghopWithoutF->recursive=-1;
       //tim dia phuong user hay toi nhat
                        $f_dis_user=$this->FulltrainTonghopWithoutF->find('first',
                            array(
                            'conditions'=>array(
                            'user_id'=>$t[$i]['Testing']['user_id'])));
       
                        $this->FulltrainsULocationTable->recursive=-1;
                        $t_2latents=$this->FulltrainsULocationTable->find('all',
                            array(
                                'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])
                        //         ,'fields'=>array('location_id','location_id')
                             ,            'order'=>array('num_checks'=>'desc')
                                ,           'limit'=>1
                                ));
                        $this->LocationNumcksTb->recursive=-1;
                        $ls_considered=$this->LocationNumcksTb->find('first',array(
                            'conditions'=>array(
                                'num_cks >= ? '=>array($hot_degree),
                                'location_id'=>$t[$i]['Testing']['location_id']),
                            'fields'=>array('x','y')));
     //  print_r($t_2latents);
   //  array_values  ($t_2latents);   
                        $k1=count($t_2latents);
  //    print_r($t_2latents);
       //xac dinh neu tim duoc thi ngat ra khoi vong lap luon
                    /** FOR 2*/
                   //     $chan=0;
                        foreach($t_2latents as $b)
                        {
       
                            $kc1=abs($this->calculateDistance(
                                 $b['FulltrainsULocationTable']['x'],
                                $ls_considered['LocationNumcksTb']['x'],
                                $b['FulltrainsULocationTable']['y'],
                                $ls_considered['LocationNumcksTb']['y']
                             ));
                            $max=0;
                  //      if($chan==0){
                            if($f_dis_user['FulltrainTonghopWithoutF']['max_move']<=10)
                                $max=10;
                            else
                                $max=$f_dis_user['FulltrainTonghopWithoutF']['max_move'];
                            
                            //$chan=1;
                       // }else{
//                            if($f_dis_user['FulltrainTonghopWithoutF']['kc']<=10)
//                                $max=10;
//                            else
//                                $max=$f_dis_user['FulltrainTonghopWithoutF']['kc'];
//                        }
            //neu khoang cach di chuyen dc so sanh voi 
            //kc-->avg move
            //max_move--> khoang cach di chuyen xa nhat
            //nho hon thi ta bao co the user se den do
            /**
             * IF SO 4
             * */
                            if($max>=$kc1)
                            {
                                $this->Testing->query("
                                    update testings set 
                                        ".$cotupdate."=1
                                        where location_id='".$t[$i]['Testing']['location_id']."'
                                            and user_id=".$t[$i]['Testing']['user_id']);
                                $ngat=1;
                                break;
                            }
                        } /** HET FOR 2*/
  
       
       //neu da tim duoc o tren thi bo qua
  /**
             * IF SO 5
             * */
                        if($ngat==0)
                        {
      
                            $this->FulltrainTonghopWithoutF->recursive=-1;
       //su dung nhung du lieu ban be, de do khoang cach di chuyen tu noi ma ban be den nhieu nhat
       //fs lay tu khai bao khuc tren
                            $f_dis=$this->FulltrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
                            $chay=count($f_dis);
                            /**  FOR 3**/
                            for($k=0;$k<$chay;$k++)
                            {
                                $khoang_cach=abs($this->calculateDistance(
                                    $f_dis[$k]['FulltrainTonghopWithoutF']['x'],
                                    $ls_considered['LocationNumcksTb']['x'],
                                    $f_dis[$k]['FulltrainTonghopWithoutF']['y'],
                                    $ls_considered['LocationNumcksTb']['y']
                                ));
                                   $max=0;
                  //      if($chan==0){
                                if($f_dis_user['FulltrainTonghopWithoutF']['max_move']<=10)
                                    $max=10;
                                else
                                    $max=$f_dis_user['FulltrainTonghopWithoutF']['max_move'];
                      
                                /** IF SO 6 * */
                                if($max>=$khoang_cach)
                                {
                                    $this->Testing->query("
                                        update testings set 
                                            ".$cotupdate."=1
                                            where location_id='".$t[$i]['Testing']['location_id']."'
                                                and user_id=".$t[$i]['Testing']['user_id']);
                                    break;
                                }/** HET IF 6**/
                                    
                                   
                            }/** HET FOR 3**/
                        }/** HET IF 5**///else{
//                            /** if 7*/
//                            foreach($h1 as $h){
//                                $kc=$this->calculateDistance(
//                                    $h['LocationNumcksTb']['x'],
//                                    $ls_considered['LocationNumcksTb']['x'],
//                                    $h['LocationNumcksTb']['y'],
//                                    $ls_considered['LocationNumcksTb']['y']
//                                    
//                                );
//                                //if 8
//                                if($kc<=$demo){
//                                    $this->Testing->query("
//                                        update testings set 
//                                            ".$cotupdate."=1
//                                            where location_id='".$t[$i]['Testing']['location_id']."'
//                                                and user_id=".$t[$i]['Testing']['user_id']);
//                                    break;
//                                    
//                                }
//                            }
//                        }
                           
                            
                          }/** HET ELSE 3**/
                 
                    }/** HET ELSE 2**/
                   
            
          
        }/** HET ELSE 1**/
        
}/** HET FOR 1**/

$kq=$this->Testing->query("
    select count(!(".$cotupdate." xor yes)) as d
    from testings
    where !(".$cotupdate." xor yes)<>0");
    
print_r($kq);
$this->Testing->query("insert into eallh(cks, dung) 
        values('".$demo."','".$kq[0][0]['d']."')");
}/** HET FOR THU NGHIEM**/
    }/** HET FUNCTION**/



/**
 * chi xet full history cua friends
 * do chinh xac 64%,14 right
 * */
    function fullhis_f_100(){
//cot de update; de tren day de co thay doi cot thi sua cho de
    $cotupdate='h_fr';

    $this->Testing->recursive=-1;
        ///13422
    $t=$this->Testing->find('all',array(
            'fields'=>array('user_id','location_id','yes')
            //,'conditions'=>array('yes'=>0)
            ));
    $n=count($t);


    for($i=0;$i<$n;$i++){
    
                $this->Friendship->recursive=-1;
                $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$t[$i]['Testing']['user_id'])));
       
                $this->Newcheckin->recursive=-1;
                $f_predicted=$this->Newcheckin->find('list',
                        array(
                        'conditions'=>array('user_id'=>$fs)
                    ,                    'fields'=>array('location_id','location_id')
                    ));
                if(in_array($t[$i]['Testing']['location_id'],$f_predicted)){
                        $this->Testing->query("update testings set 
                            ".$cotupdate."=1 
							where location_id='".$t[$i]['Testing']['location_id']."'
                                    and user_id=".$t[$i]['Testing']['user_id']."");
                }
                   
            
          
    }
        
    }
}
?>